package com.yqg.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yqg.manage.entity.RoleMenu;
import com.yqg.manage.entity.UnitMenu;
import com.yqg.manage.mapper.RoleMenuMapper;
import com.yqg.manage.mapper.UnitMenuMapper;
import com.yqg.manage.service.IRoleMenuService;
import com.yqg.manage.service.IUnitMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * UnitMenuServiceImpl 服务实现类
 * @author zxf
 * @since 2025-09-16
 */
@Service
public class UnitMenuServiceImpl extends ServiceImpl<UnitMenuMapper, UnitMenu> implements IUnitMenuService {

    @Resource
    private UnitMenuMapper unitMenuMapper;

    @Resource
    private IRoleMenuService roleMenuService;

    @Override
    public boolean saveOrUpdateUnitMenu(List<Long> menuIds, Long unitId) {
        // 处理null情况，menuIds为空表示要清除所有关联
        if (unitId == null) {
            return false; // unitId不能为空
        }
        if (menuIds == null) {
            menuIds = Collections.emptyList();
        }

        LambdaQueryWrapper<UnitMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UnitMenu::getUnitId, unitId);
        List<UnitMenu> oldDataList = list(queryWrapper);
        Set<Long> oldDataIds = oldDataList.stream()
                .map(UnitMenu::getMenuId)
                .collect(Collectors.toSet());

        // 需要新增的菜单
        Set<Long> addMenuIds = menuIds.stream()
                .filter(menuId -> !oldDataIds.contains(menuId))
                .collect(Collectors.toSet());

        // 需要删除的菜单ID
        List<Long> finalMenuIds = menuIds;
        List<Long> toDeleteIds = oldDataList.stream()
                .filter(unitMenu -> !finalMenuIds.contains(unitMenu.getMenuId()))
                .map(UnitMenu::getId)
                .collect(Collectors.toList());

        // 执行删除操作
        if (!toDeleteIds.isEmpty()) {
            this.removeByIds(toDeleteIds);

            // 同时删除这个单位角色的对应权限

            List<Long> toDeleteRoleMenuIds = oldDataList.stream()
                    .filter(unitMenu -> !finalMenuIds.contains(unitMenu.getMenuId()))
                    .map(UnitMenu::getMenuId)
                    .collect(Collectors.toList());
            roleMenuService.removeMenuIdsByUnitId(toDeleteRoleMenuIds,unitId);
        }

        // 执行新增操作
        List<UnitMenu> addDataBaseList = new ArrayList<>();
        addMenuIds.forEach(addId -> {
            UnitMenu unitMenu = new UnitMenu();
            unitMenu.setMenuId(addId);
            unitMenu.setUnitId(unitId);
            addDataBaseList.add(unitMenu);
        });
        if (unitId != 1) {
            // 新增最大菜单同时更新单位局长的权限
            roleMenuService.saveOrUpdateRoleMenu(menuIds,unitId, 2L);
        }
        return addDataBaseList.isEmpty() || this.saveBatch(addDataBaseList);
    }

}
